clear all
set more off 

cd "C:\Dropbox\Average_target_survey\replication_folder\dofiles"
quiet do load_rreg2

cd "C:\Dropbox\Average_target_survey\replication_folder\workfiles"
use step001, clear

***collapse days when the sample size is small
gen start_day2=start_day1
recode start_day2 30/32=29

gen prob_prior=	epi_12m_12plus + epi_12m_8_12 + epi_12m_4_8

tempname 1
postfile `1'  str50 variable ///
	str50 E_statistic ///
	str12 E_aug26 ///
	str12 E_aug27 ///
	str12 E_aug28 ///
	str12 E_aug29 ///
	using "C:\Dropbox\Average_target_survey\replication_folder\outregs\Appendix_Table03.dta", replace every(1)
	
*** create dummy variables for each day	
foreach var in 26 27 28 29 {	
	gen aug`var'=(start_day2==`var')
	gen _aug`var'=aug`var'*sqrt(weight)
}	


	
foreach var in 	QJH11 /// Inflation target of the Fed
				epi_12m ///
				epi_12m_mean ///
				prob_prior ///
				epi_12m_std ///
				QJH121 /// Inflation target of the Fed: inflation is 1%
				QJH122 /// Inflation target of the Fed: inflation is 3%
				egdp_12m /// 
				edy_12m ///
				{		    
					
			capture drop Y_flag 
			gen Y_flag=(abs(`var')>20)
			
			capture drop _`var'
			gen _`var'=`var'*sqrt(weight)
			
			***~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			*** raw mean
			***~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			
			reg `var' aug26 aug27 aug28 aug29 [aw=weight], robust nocons
			test aug26 = aug27
			local p1=r(p)
			test aug26 = aug28
			local p2=r(p)
			test aug26 = aug29
			local p3=r(p)			
			
			local M26_mean=string(_b[aug26],"%4.2f")
			local M27_mean=string(_b[aug27],"%4.2f")
			local M28_mean=string(_b[aug28],"%4.2f")
			local M29_mean=string(_b[aug29],"%4.2f")

			if abs(`p1')<0.1 & `p1'~=. local M27_mean="`M27_mean'" + "*"
			if abs(`p2')<0.1 & `p1'~=. local M28_mean="`M28_mean'" + "*"
			if abs(`p3')<0.1 & `p1'~=. local M29_mean="`M29_mean'" + "*"
			
			if abs(`p1')<0.05 & `p1'~=. local M27_mean="`M27_mean'" + "*"
			if abs(`p2')<0.05 & `p1'~=. local M28_mean="`M28_mean'" + "*"
			if abs(`p3')<0.05 & `p1'~=. local M29_mean="`M29_mean'" + "*"
			
			if abs(`p1')<0.01 & `p1'~=. local M27_mean="`M27_mean'" + "*"
			if abs(`p2')<0.01 & `p1'~=. local M28_mean="`M28_mean'" + "*"
			if abs(`p3')<0.01 & `p1'~=. local M29_mean="`M29_mean'" + "*"			
		
			***~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			*** raw median
			***~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			
			qreg `var'  aug27 aug28 aug29 [iw=weight]
			test aug27
			local p1=r(p)
			test aug28
			local p2=r(p)
			test aug29
			local p3=r(p)			
			
			local M26_median=string(_b[_cons],"%4.2f")
			local M27_median=string(_b[_cons]+_b[aug27],"%4.2f")
			local M28_median=string(_b[_cons]+_b[aug28],"%4.2f")
			local M29_median=string(_b[_cons]+_b[aug29],"%4.2f")

			if abs(`p1')<0.1 & `p1'~=. local M27_median="`M27_median'" + "*"
			if abs(`p2')<0.1 & `p1'~=. local M28_median="`M28_median'" + "*"
			if abs(`p3')<0.1 & `p1'~=. local M29_median="`M29_median'" + "*"
			
			if abs(`p1')<0.05 & `p1'~=. local M27_median="`M27_median'" + "*"
			if abs(`p2')<0.05 & `p1'~=. local M28_median="`M28_median'" + "*"
			if abs(`p3')<0.05 & `p1'~=. local M29_median="`M29_median'" + "*"
			
			if abs(`p1')<0.01 & `p1'~=. local M27_median="`M27_median'" + "*"
			if abs(`p2')<0.01 & `p1'~=. local M28_median="`M28_median'" + "*"
			if abs(`p3')<0.01 & `p1'~=. local M29_median="`M29_median'" + "*"
			

			***~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			*** restricted mean (exclude people outside [-20,20])
			***~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			
			reg `var' aug26 aug27 aug28 aug29 [aw=weight] if Y_flag==0, robust nocons
			test aug26 = aug27
			local p1=r(p)
			test aug26 = aug28
			local p2=r(p)
			test aug26 = aug29
			local p3=r(p)			
			
			local M26_mean_r=string(_b[aug26],"%4.2f")
			local M27_mean_r=string(_b[aug27],"%4.2f")
			local M28_mean_r=string(_b[aug28],"%4.2f")
			local M29_mean_r=string(_b[aug29],"%4.2f")

			if abs(`p1')<0.1 & `p1'~=. local M27_mean_r="`M27_mean_r'" + "*"
			if abs(`p2')<0.1 & `p1'~=. local M28_mean_r="`M28_mean_r'" + "*"
			if abs(`p3')<0.1 & `p1'~=. local M29_mean_r="`M29_mean_r'" + "*"
			
			if abs(`p1')<0.05 & `p1'~=. local M27_mean_r="`M27_mean_r'" + "*"
			if abs(`p2')<0.05 & `p1'~=. local M28_mean_r="`M28_mean_r'" + "*"
			if abs(`p3')<0.05 & `p1'~=. local M29_mean_r="`M29_mean_r'" + "*"
			
			if abs(`p1')<0.01 & `p1'~=. local M27_mean_r="`M27_mean_r'" + "*"
			if abs(`p2')<0.01 & `p1'~=. local M28_mean_r="`M28_mean_r'" + "*"
			if abs(`p3')<0.01 & `p1'~=. local M29_mean_r="`M29_mean_r'" + "*"
			
			***~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			*** share of people with unreasonable expectations (outside [-20,20])
			***~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			
			reg Y_flag aug26 aug27 aug28 aug29 [aw=weight], robust nocons
			test aug26 = aug27
			local p1=r(p)
			test aug26 = aug28
			local p2=r(p)
			test aug26 = aug29
			local p3=r(p)			
			
			local M26_mean_s=string(_b[aug26],"%4.2f")
			local M27_mean_s=string(_b[aug27],"%4.2f")
			local M28_mean_s=string(_b[aug28],"%4.2f")
			local M29_mean_s=string(_b[aug29],"%4.2f")

			if abs(`p1')<0.1 & `p1'~=. local M27_mean_s="`M27_mean_s'" + "*"
			if abs(`p2')<0.1 & `p1'~=. local M28_mean_s="`M28_mean_s'" + "*"
			if abs(`p3')<0.1 & `p1'~=. local M29_mean_s="`M29_mean_s'" + "*"
			
			if abs(`p1')<0.05 & `p1'~=. local M27_mean_s="`M27_mean_s'" + "*"
			if abs(`p2')<0.05 & `p1'~=. local M28_mean_s="`M28_mean_s'" + "*"
			if abs(`p3')<0.05 & `p1'~=. local M29_mean_s="`M29_mean_s'" + "*"
			
			if abs(`p1')<0.01 & `p1'~=. local M27_mean_s="`M27_mean_s'" + "*"
			if abs(`p2')<0.01 & `p1'~=. local M28_mean_s="`M28_mean_s'" + "*"
			if abs(`p3')<0.01 & `p1'~=. local M29_mean_s="`M29_mean_s'" + "*"
			
			***~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			*** Huber-robust mean 
			***~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			capture drop wgt_Y
			rreg2 _`var' _aug26 _aug27 _aug28 _aug29, nolog gen(wgt_Y) tune(3)
			replace wgt_Y=wgt_Y*weight
			reg `var' aug26 aug27 aug28 aug29 [aw=wgt_Y], robust nocons
			
			test aug26 = aug27
			local p1=r(p)
			test aug26 = aug28
			local p2=r(p)
			test aug26 = aug29
			local p3=r(p)			
			
			local M26_mean_huber=string(_b[aug26],"%4.2f")
			local M27_mean_huber=string(_b[aug27],"%4.2f")
			local M28_mean_huber=string(_b[aug28],"%4.2f")
			local M29_mean_huber=string(_b[aug29],"%4.2f")

			if abs(`p1')<0.1 & `p1'~=. local M27_mean_huber="`M27_mean_huber'" + "*"
			if abs(`p2')<0.1 & `p1'~=. local M28_mean_huber="`M28_mean_huber'" + "*"
			if abs(`p3')<0.1 & `p1'~=. local M29_mean_huber="`M29_mean_huber'" + "*"
			
			if abs(`p1')<0.05 & `p1'~=. local M27_mean_huber="`M27_mean_huber'" + "*"
			if abs(`p2')<0.05 & `p1'~=. local M28_mean_huber="`M28_mean_huber'" + "*"
			if abs(`p3')<0.05 & `p1'~=. local M29_mean_huber="`M29_mean_huber'" + "*"
			
			if abs(`p1')<0.01 local M27_mean_huber="`M27_mean_huber'" + "*"
			if abs(`p2')<0.01 local M28_mean_huber="`M28_mean_huber'" + "*"
			if abs(`p3')<0.01 local M29_mean_huber="`M29_mean_huber'" + "*"			
			
			***~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			*** post results
			***~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			post `1' ("`var'") ("raw mean")	("`M26_mean'") ("`M27_mean'") ("`M28_mean'")  ("`M29_mean'")  
			post `1' (" ") ("raw median")	("`M26_median'") ("`M27_median'") ("`M28_median'")  ("`M29_median'")  
			post `1' (" ") ("restricted mean")	("`M26_mean_r'") ("`M27_mean_r'") ("`M28_mean_r'")  ("`M29_mean_r'")  
			post `1' (" ") ("share with unreasonable responses")	("`M26_mean_s'") ("`M27_mean_s'") ("`M28_mean_s'")  ("`M29_mean_s'")  
			post `1' (" ") ("Huber mean")	("`M26_mean_huber'") ("`M27_mean_huber'") ("`M28_mean_huber'")  ("`M29_mean_huber'")  
			
}

postclose `1'

tab start_day2
